package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.TravelCommentQuery;
import cn.wolfcode.wolf2w.repository.TravelCommentRepository;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TravelCommentServiceImpl implements ITravelCommentService {

    @Autowired
    private MongoTemplate template;
    @Autowired
    private TravelCommentRepository travelCommentRepository;


    @Override
    public TravelComment get(String id) {
        return travelCommentRepository.findById(id).get();
    }

    @Override
    public List<TravelComment> selectAll() {
        return travelCommentRepository.findAll();
    }

    @Override
    public Page<TravelComment> query(TravelCommentQuery qo) {
        //创建Query对象,根据过滤条件查询总条数,此处没有限制条件,所以用空的查询对象查
        /*Query query = new Query();
        //过滤
        query.addCriteria(Criteria.where("strategyId").is(qo.getStrategyId()));
        //分页:按照时间倒序
        Pageable pageAble = PageRequest.of(qo.getCurrentPage() - 1,
                qo.getPageSize(), Sort.Direction.DESC, "createTime");
        //返回给控制器
        Page<TravelComment> page = DBHelper.query(template, TravelComment.class, query, pageAble);
        return page;*/
        return null;
    }

    @Override
    public void update(TravelComment travelComment) {
        travelCommentRepository.save(travelComment);
    }

    @Override
    public void save(TravelComment travelComment) {
        travelComment.setId(null);
        travelCommentRepository.save(travelComment);
    }

    @Override
    public void deleteById(String id) {
        travelCommentRepository.deleteById(id);
    }


    @Override
    public void saveOrUpdate(TravelComment travelComment, UserInfo userInfo) {

    }

    @Override
    public List<TravelComment> comments(String travelId) {
        //查
        List<TravelComment> travelComments = this.selectAll();
        return travelComments;
    }

    @Override
    public void commentAdd(TravelComment travelComment, UserInfo userInfo) {
        /**
         travelTitle: 广州三天三夜|跟随必字榜吃喝玩乐耍
         content: 1511545454561510
         type: 1
         refComment.id: 5eabf1aa79588d419a85ac43
         缺少字段:  user相关  创建时间    关联的评论对象
         */
        travelComment.setId(null);
        BeanUtils.copyProperties(userInfo, travelComment);
        travelComment.setUserId(userInfo.getId());
        travelComment.setCreateTime(new Date());
        //type为0普通评论,type为1评论的评论
        if (travelComment.getType()==TravelComment.TRAVLE_COMMENT_TYPE){
            //如果为普通评论,不需要加关联的评论对象
        }else {
            //如果为评论的评论,则需要加关联的评论对象
            TravelComment ref = this.get(travelComment.getRefComment().getId());
            travelComment.setRefComment(ref);
        }
        //更新
        this.save(travelComment);
    }
}
